package ai.people.netmon.warning.controller;

import ai.people.core.common.util.ApplicationContextUtil;
import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.domain.warning.entity.WarningAccountCollect;
import ai.people.netmon.framework.domain.warning.entity.WarningInfoCollect;
import ai.people.netmon.framework.domain.warning.request.collect.AccountCollectionSaveRequest;
import ai.people.netmon.framework.domain.warning.request.collect.InfoCollectionSaveRequest;
import ai.people.netmon.framework.domain.warning.request.collect.MessageContentSaveVO;
import ai.people.netmon.framework.domain.warning.vo.WarningAccountVO;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.warning.dict.DictGrpcClient;
import ai.people.netmon.warning.service.WarningAccountCollectService;
import ai.people.netmon.warning.service.WarningInfoCollectService;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 收藏相关接口
 *
 * @author yuanqinglong
 * @date 2022/7/27 14:40
 */
@Validated
@RestController
@RequestMapping("/v1/collect")
public class WarningCollectController {

    @Autowired
    private WarningInfoCollectService warningInfoCollectService;

    @Autowired
    private WarningAccountCollectService warningAccountCollectService;

    /**
     * 信息收藏/取消收藏
     *
     * @param type          操作类型，1收藏，2取消收藏
     * @param collectSource 收集来源 1:信息预警、2:主题舆情、3:账号言论
     * @param reqParam      要求参数
     * @return {@link Result}<{@link Integer}>
     */
    @PostMapping("/info/{type}/{collectSource}")
    public Result<Integer> infoCollect(@PathVariable("type") Integer type,
                                       @PathVariable(value = "collectSource", required = false) Integer collectSource,
                                       @Validated @RequestBody InfoCollectionSaveRequest reqParam) {

        DictGrpcClient dictService = ApplicationContextUtil.getBean(DictGrpcClient.class);
        Map<String, String> platformNameMap = dictService.getDictMap("warning_message_source");
        HashMap<String, String> newPlatformNameMap = Maps.newHashMap();
        platformNameMap.keySet().forEach(o-> newPlatformNameMap.put(platformNameMap.get(o), o));

        if(reqParam.getMessageContent() == null){
            MessageContentSaveVO saveVO = new MessageContentSaveVO();
            saveVO.setId(reqParam.getId());
            saveVO.setPlatform(newPlatformNameMap.getOrDefault(reqParam.getPlatform(), reqParam.getPlatform()));
            reqParam.setMessageContent(saveVO);
        }
        WarningInfoCollect infoCollect = WarningInfoCollect.builder()
                .collectUserId(RequestHolderUtil.getUserId())
                .collectInfoPlatform(reqParam.getMessageContent().getPlatform())
                .collectInfoId(reqParam.getMessageContent().getId())
                .collectSource(collectSource)
                .collectInfoAccountTitle(reqParam.getMessageContent().getTitle())
                .collectInfoContent(reqParam.getMessageContent().getContent())
                .collectInfoEmotion(reqParam.getMessageContent().getEmotion())
                .collectInfoLabel(reqParam.getMessageContent().getLabel())
                .collectInfoReleaseTime(reqParam.getMessageContent().getTime())
                .build();
        if(type == 1){
            return ResultUtil.success(warningInfoCollectService.infoCollect(infoCollect, JSON.toJSONString(reqParam.getMessageContent())));
        }else if(type == 2){
            return ResultUtil.success(warningInfoCollectService.infoCancelCollect(infoCollect));
        }
        return ResultUtil.success(0);
    }

    /**
     * 账号收藏/取消收藏
     *
     * @param type          操作类型，1收藏，2取消收藏
     * @param collectSource 收集来源 1:账号预警
     * @param reqParam      要求参数
     * @return {@link Result}<{@link Integer}>
     */
    @PostMapping("/account/{type}/{collectSource}")
    public Result<Integer> accountCollect(@PathVariable("type") Integer type,
                                          @PathVariable(value = "collectSource", required = false) Integer collectSource,
                                          @Validated @RequestBody AccountCollectionSaveRequest reqParam) {

        DictGrpcClient dictService = ApplicationContextUtil.getBean(DictGrpcClient.class);
        Map<String, String> platformNameMap = dictService.getDictMap("warning_message_source");
        HashMap<String, String> newPlatformNameMap = Maps.newHashMap();
        platformNameMap.keySet().forEach(o-> newPlatformNameMap.put(platformNameMap.get(o), o));

        if(reqParam.getWarningAccount() == null){
            WarningAccountVO saveVO = new WarningAccountVO();
            saveVO.setId(reqParam.getId());
            saveVO.setPlatform(newPlatformNameMap.getOrDefault(reqParam.getPlatform(), reqParam.getPlatform()));
            reqParam.setWarningAccount(saveVO);
        }
        WarningAccountCollect accountCollect = WarningAccountCollect.builder()
                .collectUserId(RequestHolderUtil.getUserId())
                .collectAccountPlatform(reqParam.getWarningAccount().getPlatform())
                .collectAccountId(reqParam.getWarningAccount().getAccountId())
                .collectSource(collectSource)
                .collectAccountName(reqParam.getWarningAccount().getAccountNickName())
                .collectAccountDescription(reqParam.getWarningAccount().getAccountSummary())
                .collectAccountHeadImage(reqParam.getWarningAccount().getAccountAvatar())
                .collectAccountLabel(reqParam.getWarningAccount().getAccountLabel())
                .collectAccountUrl(reqParam.getWarningAccount().getAccountLink())
                .build();
        if(type == 1){
            return ResultUtil.success(warningAccountCollectService.accountCollect(accountCollect, JSON.toJSONString(reqParam.getWarningAccount())));
        }else if(type == 2){
            return ResultUtil.success(warningAccountCollectService.accountCancelCollect(accountCollect));
        }
        return ResultUtil.success(0);
    }
}
